package com.yingda.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.yingda.ad.Constants;
import com.yingda.ad.R;
import com.yingda.bean.WorkBean;
import com.yingda.eventbus.EventBus;
import com.yingda.utils.ExcelUtils;
import com.yingda.utils.ExecutorsUtils;
import com.yingda.utils.RegexUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class ReadTabFragment extends Fragment {
    public static final String TAG = "ReadTabFragment";
    private List<WorkBean>  workBeanListFist = new ArrayList<>();
    private HashMap<String, WorkBean> workBeanListFist1 = new HashMap<>();
    private StringBuffer sb = new StringBuffer();
    private TextView tv_content;
    private Handler handler = new Handler(Looper.getMainLooper());

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_table, container, false);
        initView(view);
        initData();
        return view;
    }

    private void initData() {



        ExecutorsUtils.getInstance().getSingleThread(() -> {
            try {
                InputStream inputStream = getActivity().getAssets().open("111.xls");
                readExcel(inputStream,new File("111.xls"));

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }


    //读取excel 的数据
    public void readExcel(File excelPath){
        // String excelPath1 = SharedPreferenceUtil.getInstance(this).getString("excelPath");
//        List<List<Object>> lists = ExcelUtils.getInstance().readExcel(excelPath);
//        Log.i(TAG, "readExcel: " + lists);
    }

    public void  readExcel(InputStream inputStream ,File excelPath){
        // String excelPath1 = SharedPreferenceUtil.getInstance(this).getString("excelPath");
        List<List<Object>> lists = ExcelUtils.getInstance().readExcel(inputStream,excelPath);
        Log.i(TAG, "readExcel: " + lists);
        workBeanListFist.clear();
        workBeanListFist1.clear();
        for (List<Object> list : lists) {
            if (list!=null && list.size()>0){
                for (Object o : list) {
                    //打印每一行的信息
                    if (o!=null){
                        List<WorkBean> workBeans = RegexUtils.getInstance().regexWord(o.toString());
                        if (workBeans!=null && workBeans.size()>0){
                            workBeanListFist.addAll(workBeans);
                        }
                    }

                }
            }
        }
        Log.i(TAG, "readExcel: " + workBeanListFist);
        if (workBeanListFist.size()>0){
            for (WorkBean workBean : workBeanListFist) {
                String name = workBean.getName();
                String id = workBean.getId();
                sb.append("name=" + name + "...id=" + id + "\n");
                workBeanListFist1.put(id,workBean);
            }
            handler.post(() -> {
                EventBus.getDefault().post("关闭dialog", Constants.DIALOGCLOSE);
                tv_content.setText(sb.toString());
            });
        }else {
            handler.post(() -> EventBus.getDefault().post("关闭dialog", Constants.DIALOGCLOSE));
        }

        //传递数据
        EventBus.getDefault().post(workBeanListFist1,Constants.READONE);
    }


    private void initView(View view) {
        tv_content = view.findViewById(R.id.tv_content);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }
}
